package  com.hx.base.servlet.velocity;
import java.io.StringWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.VelocityResult;
import org.apache.struts2.views.JspSupportServlet;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
public class VelocityRedirectResult extends VelocityResult{

    private static final long serialVersionUID = 1L;
    private static final String REDIRECT_URL = "_action_redirect_url";
    private static final Logger LOG = LoggerFactory.getLogger(VelocityRedirectResult.class);
    public VelocityRedirectResult() {
        super();
    }
    public VelocityRedirectResult(String location) {
        super(location);
    }
   
    private VelocityManager velocityManager;
    @Inject
    public void setVelocityManager(VelocityManager mgr) {
        this.velocityManager = mgr;
    }
    @Override
    public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
    	ValueStack stack = ActionContext.getContext().getValueStack();
    	HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        ServletContext servletContext = ServletActionContext.getServletContext();
        velocityManager.init(servletContext);
        
        JspFactory jspFactory = null;
        Servlet servlet = JspSupportServlet.jspSupportServlet;
      
        boolean usedJspFactory = false;
        PageContext pageContext = (PageContext) ActionContext.getContext().get(ServletActionContext.PAGE_CONTEXT);
        if (pageContext == null && servlet != null) {
            jspFactory = JspFactory.getDefaultFactory();
            pageContext = jspFactory.getPageContext(servlet, request, response, null, true, 8192, true);
            ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT, pageContext);
            usedJspFactory = true;
        }
        String redirectUrl = null;
        try {
            String encoding = getEncoding(finalLocation);
            String contentType = getContentType(finalLocation);

            if (encoding != null) {
                contentType = contentType + ";charset=" + encoding;
            }
            Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);
            StringWriter sw = new StringWriter();
            Context context = createContext(velocityManager, stack, request, response, finalLocation);
            response.setContentType(contentType);
            t.merge(context, sw);
            
            redirectUrl = (String)request.getAttribute(REDIRECT_URL);
            if(redirectUrl == null || "".equals(redirectUrl)){
               redirectUrl = finalLocation;
            }
            redirectUrl = request.getContextPath() + redirectUrl ;
            response.sendRedirect(redirectUrl);
        } catch (Exception e) {
            LOG.error("Unable to response  sendRedirect, '" + redirectUrl + "'", e);
            throw e;
        } finally {
            if (usedJspFactory) {
                jspFactory.releasePageContext(pageContext);
            }
        }

        return;
    }
}